import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2025-05-12
 * Time: 15:30
 */
public class AddMethod {
    private Scanner input;
    // 创建一个静态方法接收字符串和返回求和值
    public static int sum (String input) {
        if (input == null || input.isEmpty()) {
            return 0;
        }
        int sum = 0;

        String[] parts = input.split(",");

        for (String part : parts) {
            try {
                sum += Integer.parseInt(part.trim());
            }catch (NumberFormatException e) {
                throw new IllegalArgumentException("无效数字: " + part, e);
            }
        }
        return sum;
    }
    public static void main(String[] args) {
       try {
           System.out.println(sum("1,2,3"));

           System.out.println(sum("1,a,3"));
       }catch (IllegalArgumentException e) {
           System.err.println("错误: " + e.getMessage());
       }
    }

}
